package ooday04exercise02;

public class Test {
    public static void main(String[] args) {
//        Animal o=new Animal() ;抽象类不能被实例化
        //多态的应用1：把几个对象封装到一个数组中，实现代码的复用
        Animal[] animal=new Animal[3];
        animal[0]=new Dog("大黄",2,"黄色");//向上造型
        animal[1]=new Fish("金金",2,"金色");
        animal[2]=new Chick("小黄鸡",2,"黄色");
        for (int i=0;i<animal.length;i++){
            System.out.println(animal[i].name);
            animal[i].drink();
            animal[i].eat();
            if(animal[i] instanceof Dog){
                Dog dog=(Dog) animal[i];
                dog.lookHome();
            }
            if (animal[i] instanceof Chick){
                Chick chick=(Chick) animal[i];
                chick.layEggs();
            }
            if (animal[i] instanceof  Swim){
                Swim swim=(Swim) animal[i];
                swim.swim();
            }

        }
        //多态的应用2：把超类型作为参数或者返回值，传递或者返回派生类对象，扩大方法的范围，实现代码复用
        Master master=new Master();
        Dog dog=new Dog("大黄",2,"黄色");
        Fish fish=new Fish("金金",1,"金色");
        Chick chick=new Chick("公鸡",2,"红色");
        master.feed(dog);
        master.feed(fish);
        master.feed(chick);

        Animal o=new Dog("小黑",1,"黑色");//向上造型
        Dog dog2=(Dog) o;//引用指向Dog类对象
//        Fish fish2=(Fish) o;
        Swim swim=(Swim)o;//引用指向的对象实现了Swim接口
        System.out.println(o instanceof Dog);//true
        System.out.println(o instanceof Fish);//false
        System.out.println(o instanceof Swim);//true


    }
}
